package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneFuncProdInstService;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalBigBandwidthAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月28日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(LocalBigBandwidthAttrHandler.class)
public class LocalBigBandwidtAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    IOperateSceneFuncProdInstService operateSceneFuncProdInstService;


    /**
     * 是否大带宽为是时才展示附件上传
     */
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (Objects.isNull(attr)) {
            return;
        }
        // attachments  新增附件信息
        KiteAttr bigBandwidAttachmentthAttr = inst.getAttr("300000203");
        if (Objects.isNull(bigBandwidAttachmentthAttr)) {
            return;
        }
        String sceneInstId = data.getSceneInstId();
        //附件信息
        if(KiteStringUtils.isNotEmpty(attr.getValue())&& KiteStringUtils.equalsAny(attr.getValue(),"1_0000106" )){
            bigBandwidAttachmentthAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
        }else {
            bigBandwidAttachmentthAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
            bigBandwidAttachmentthAttr.setValue("");
            bigBandwidAttachmentthAttr.setValueDesc("");
            if (KiteStringUtils.equals(attr.getValue(), "1_0000107")) {
                List<KiteProdInstFunc> prodFuncList = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, attr.getGroupId());
                prodFuncList.forEach(prodFunc -> {
                    operateSceneFuncProdInstService.removeFuncProdInst(sceneInstId, inst.getInstId(), prodFunc.getProdInstId(),
                        data.getCustId(), ContextUtil.getSimpleLoginInfo().getStaffId().toString());
                });
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
